%im=imread('demo_small.png');
im=imread('sample.transformed.jpg');
figure,imshow(im);

%im_red=im(:,:,1);
%figure,imshow(im_red);
%im_gray=rgb2gray(im);
%figure,imshow(im_gray);
%im_diff=imsubtract(im_red,im_gray);
%figure,imshow(im_diff);

% This is the convolution kernel.
%f1=fspecial('average',3);

% This is 'soebel' kernel
f1=[-1,0,1;-3,0,3;-1,0,1];

r1=uint8(filter2(f1,double(im(:,:,1)),'same'));
r2=uint8(filter2(f1,double(im(:,:,2)),'same'));
r3=uint8(filter2(f1,double(im(:,:,3)),'same'));

imf(:,:,1)=r1;
imf(:,:,2)=r2;
imf(:,:,3)=r3;

figure,imshow(imf);

imG=rgb2gray(imf);
figure,imshow(imG);
imB=imG>90;
figure,imshow(imB);
imBT=imB';
figure,imshow(imBT);

for r=100:150
	RES(r,1) = r;
	RES(r,2) = 0;
	RES(r,3) = 0;
	RES(r,4) = 0;
	for c=2:columns(imBT)
		if( bitxor(imBT(r,c-1),imBT(r,c)))
			RES(r,2)+=1;
		endif
		if( imBT(r,c) == 0 )
			RES(r,3)+=1;
		else
			RES(r,4)+=1;
		endif
	endfor
endfor

result=RES(100:150,:);

for i=1:rows(result)
	if( result(i,2) > 0 )
		result(i,5)=uint8(result(i,4)/result(i,2));
	else
		result(i,5)=0;
	endif
endfor



